﻿using AspNetTraining.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspNetTraining
{
    public partial class GroupUserDetail : System.Web.UI.Page
    {
        GroupUser gruser = new GroupUser();
        public UserAccount SessionLogin
        {
            get
            {
                return (UserAccount)Session["LoggedUser"];
            }
            set
            {
                Session["LoggedUser"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // load giu lieu len combobox         
            GroupUserManager groupuser = new GroupUserManager();
           
            if (Request["id"] != null)
            {
                //luu lai user cu.
                gruser = groupuser.GetGroupUserByID(new Guid(Request["id"]));
                if (!IsPostBack)
                {
                    // set gia tri len form 
                    SetValue(gruser);
                }
            }
            else
            {
                btnUpdate.Text = "Create";              
            }  
        }
        public void SetValue(GroupUser groupuser)
        {
            txtGroupName.Text = groupuser.GroupName;
            txtCreatedOn.Text = groupuser.CreatedOn.ToString();
            txtModifiedOn.Text = groupuser.ModifiedOn.ToString();
            chkActive.Checked = (bool)groupuser.Active;
            chkIsAdmin.Checked = (bool)groupuser.IsAdmin;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            GroupUser groupuser = new GroupUser();
            GroupUserManager managergroup = new GroupUserManager();
            groupuser.GroupName = txtGroupName.Text;          
            groupuser.IsAdmin = chkIsAdmin.Checked;
            groupuser.Active = chkActive.Checked;
            if (Request["id"] != null)
            {
                groupuser.GroupUserID = gruser.GroupUserID;
                groupuser.CreatedOn = gruser.CreatedOn;
                groupuser.CreatedBy = gruser.CreatedBy;
                groupuser.ModifiedOn = DateTime.Now;
                groupuser.ModifiedBy = SessionLogin.UserAccountID;
                managergroup.UpdateGroupUser(groupuser);
                Response.Redirect("~/GroupUsers.aspx");
            }
            else
            {
                groupuser.GroupUserID = Guid.NewGuid();
                groupuser.CreatedOn = DateTime.Now;
                groupuser.CreatedBy = SessionLogin.CreatedBy;
                managergroup.InsertGroupUser(groupuser);
                Response.Redirect("~/GroupUsers.aspx");
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/GroupUsers.aspx");
        }
      
    }
}